# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.pro>

_realname=helix
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=23.05
pkgrel=2
pkgdesc="A post-modern modal text editor (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/helix-editor/helix"
license=('spdx:MPL-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-mdbook"
             "git")
source=("$_realname-$pkgver.tar.gz::$url/archive/$pkgver.tar.gz"
        'helix.sh')
noextract=("${_realname}-${pkgver}.tar.gz")
sha256sums=('65da9de40ebd275a9be9a2491b7ec35f1aec9e9259e95216ad7f812d2b8627f9'
            'c557a1e313586decf3d00fcf67e9a3f6139a2850b2e267b14d33e45636e84790')

prepare() {
  cd "${srcdir}"
  tar xf "${_realname}-${pkgver}.tar.gz" \
    --exclude=contrib/themes

  cd "${_realname}-${pkgver}"
  cp -a "runtime/themes" "contrib/themes"

  sed -i "s|hx|helix|g" contrib/completion/hx.*
  cargo vendor \
    --locked \
    --versioned-dirs

  mkdir -p .cargo
  cat >> .cargo/config.toml <<END
[source.crates-io]
replace-with = "vendored-sources"

[source.vendored-sources]
directory = "vendor"
END


  sed -i -e 's/"cfg(all(target_arch = \\"\([^\\]\+\)\\", target_env = \\"gnu\\", target_abi = \\"llvm\\", not(windows_raw_dylib)))"/"\1-pc-windows-gnullvm"/g' \
    "vendor/windows-targets-0.48.0/Cargo.toml"
  sed -i -e 's/\("Cargo.toml":\)"[^"]\+"/\1"'"$(sha256sum "vendor/windows-targets-0.48.0/Cargo.toml" | cut -d' ' -f1)"'"/' \
    "vendor/windows-targets-0.48.0/.cargo-checksum.json"
}

build() {
  cd "${srcdir}"
  rm -rf "build-${MSYSTEM}" | true
  cp -r "${_realname}-${pkgver}" "build-${MSYSTEM}"
  cd "build-${MSYSTEM}"

  CXXFLAGS+=" -Wno-implicit-int"
  "${MINGW_PREFIX}/bin/cargo.exe" build \
    --locked \
    --release

  msg "Build documentation"
  cd book

  "${MINGW_PREFIX}/bin/mdbook.exe" build
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  "${MINGW_PREFIX}/bin/cargo.exe" test \
    --locked \
    --release
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  install -Dm755 "../${_realname}.sh" "${pkgdir}${MINGW_PREFIX}/bin/${_realname}"
  install -Dm755 "target/release/hx.exe" "${pkgdir}${MINGW_PREFIX}/lib/${_realname}/hx.exe"

  local runtime_dir="${pkgdir}${MINGW_PREFIX}/var/lib/${_realname}/runtime"
  mkdir -p "$runtime_dir/grammars"
  cp -r "runtime/queries" "$runtime_dir"
  cp -r "runtime/themes" "$runtime_dir"
  find "runtime/grammars" -type f -name '*.dll' -exec \
    install -Dm 755 {} -t "$runtime_dir/grammars" \;
  install -Dm644 runtime/tutor -t "$runtime_dir"
  cp -a "${runtime_dir}/" "${pkgdir}${MINGW_PREFIX}/lib/${_realname}/runtime/"

  install -Dm644 "contrib/completion/hx.bash" "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/${_realname}"
  install -Dm644 "contrib/completion/hx.fish" "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/${_realname}.fish"
  install -Dm644 "contrib/completion/hx.zsh" "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_${_pkgname}"

  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
  cp -a "book/book" "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/html"

  install -Dm644 "LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

